<?php

namespace backend\models;


use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Activity;

/**
 * ActivitySearch represents the model behind the search form of `common\models\Activity`.
 */
class ActivitySearch extends Activity
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'activity_catalog', 'peoples', 'start_user_type', 'join_user_type', 'start_time', 'end_time', 'status'], 'integer'],
            [['name', 'banner', 'goods', 'description'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Activity::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => ['defaultOrder' => ['id' => SORT_DESC]], // 新增配置项 默认 id 倒序
            'pagination'=>[
                'pageSize'=>20,

            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        if($this->name){$query->andFilterWhere(['like','name',$this->name]);}
        if($this->activity_catalog){$query->andFilterWhere(['activity_catalog'=>$this->activity_catalog]);}
        if($this->start_time){$query->andFilterWhere(['>=','start_time',$this->start_time]);}
        if($this->end_time){$query->andFilterWhere(['<=','end_time',$this->end_time]);}
        if($this->status){$query->andFilterWhere(['status'=>$this->status]);}

//        var_dump($query->createCommand()->getSql());
        return $dataProvider;
    }
}
